package
{
	import flash.display.Graphics;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;

	public class Map
	{
		public var rowCount:uint;
		public var columnCount:uint;
		public var mark:Vector.<Vector.<int>>;
		public var objectList:Vector.<MapObject> = new Vector.<MapObject>();
		public var moduleList:Vector.<Module> = new Vector.<Module>();
		public var mapModuleList:Vector.<MapModule> = new Vector.<MapModule>();
		public var offsetX:int;
		public var offsetY:int;
		public var viewTerrain:Boolean = true;
		public function Map()
		{
			
		}
		public function Draw(g:Graphics):void
		{

		}
		public function SetValueFromSize(row:uint,column:uint):void
		{
			rowCount = row;
			columnCount = column;
			moduleList = new Vector.<Module>();
			mapModuleList = new Vector.<MapModule>();
			mark = new Vector.<Vector.<int>>(rowCount);
			for(var i:int = 0;i<rowCount;i++)
			{
				mark[i] = new Vector.<int>(columnCount);
			}
		}

		public function SetValueFromStream(data:FileStream, mapImageList:Vector.<MapImage>, objectSet:Vector.<MapObject>):void
		{
			rowCount = data.readUnsignedByte();
			columnCount = data.readUnsignedByte();
			mark = new Vector.<Vector.<int>>(rowCount);
			for(var i:int = 0;i<rowCount;i++)
			{
				mark[i] = new Vector.<int>(columnCount);
				for(var j:int = 0;j<columnCount;j++)
				{
					mark[i][j] = data.readByte();
				}
			}
			
			moduleList = new Vector.<Module>();
			var moduleCount:uint = data.readUnsignedInt();
			for(i = 0;i<moduleCount;i++)
			{
				var module:Module = new Module();
				module.id = data.readUnsignedInt();
				var mapImageID:uint = data.readUnsignedInt();
				for(j = 0;j<mapImageList.length;j++)
				{
					if(mapImageID == mapImageList[j].id)
					{
						module.mapImage = mapImageList[j];
						break;
					}
				}
				module.x = data.readUnsignedInt();
				module.y = data.readUnsignedInt();
				module.width = data.readUnsignedInt();
				module.height = data.readUnsignedInt();
				moduleList.push(module);
			}
			mapModuleList = new Vector.<MapModule>();
			var mapModuleCount:uint = data.readUnsignedInt();
			for(i = 0;i<mapModuleCount;i++)
			{
				var mapModule:MapModule = new MapModule();
				var moduleID:uint = data.readUnsignedInt();
				for(j = 0;j<moduleList.length;j++)
				{
					if(moduleID == moduleList[j].id)
					{
						mapModule.module = moduleList[j];
						break;
					}
				}
				mapModule.x = data.readInt();
				mapModule.y = data.readInt();
				mapModuleList.push(mapModule);
				if(mapModule.module.mapImage.bitmapData == null)
				{
					mapModule.module.mapImage.Load();
				}
			}
			
			var objectCount:uint = data.readUnsignedInt();
			for(i = 0;i<objectCount;i++)
			{
				var object:MapObject;
				var objectID:uint = data.readUnsignedInt();
				for(j = 0;j<objectSet.length;j++)
				{
					if(objectID == objectSet[j].id)
					{
						if(objectSet[j].GetType() == MapObject.BitmapObject)
						{
							object = new MapObjectBitmap(objectID,objectSet[j].path);
							object.SetDisplayData(objectSet[j].GetDisplayData());
						}
						else
						{
							
						}
						break;
					}
				}
				object.x = data.readInt();
				object.y = data.readInt();
				objectList.push(object);
			}
			
		}
		public function SetValueFromByteArray(data:ByteArray):void
		{
			
		}
		public function Save(stream:FileStream):void
		{
			stream.writeByte(rowCount);
			stream.writeByte(columnCount);
			for(var i:int = 0;i<rowCount;i++)
			{
				for(var j:int = 0;j<columnCount;j++)
				{
					stream.writeByte(mark[i][j]);
				}
			}
			stream.writeUnsignedInt(moduleList.length);
			for(i = 0;i<moduleList.length;i++)
			{
				stream.writeUnsignedInt(moduleList[i].id);
				stream.writeUnsignedInt(moduleList[i].mapImage.id);
				stream.writeUnsignedInt(moduleList[i].x);
				stream.writeUnsignedInt(moduleList[i].y);
				stream.writeUnsignedInt(moduleList[i].width);
				stream.writeUnsignedInt(moduleList[i].height);
			}
			stream.writeUnsignedInt(mapModuleList.length);
			for(i = 0;i<mapModuleList.length;i++)
			{
				stream.writeUnsignedInt(mapModuleList[i].module.id);
				stream.writeInt(mapModuleList[i].x);
				stream.writeInt(mapModuleList[i].y);
			}
			stream.writeUnsignedInt(objectList.length);
			for(i = 0;i<objectList.length;i++)
			{
				stream.writeUnsignedInt(objectList[i].id);
				stream.writeInt(objectList[i].x);
				stream.writeInt(objectList[i].y);
			}
			
		}
		
	}
}